{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "68fc852df94942f0917b4e3a1e5f21de",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=0.25, description='conf', max=1.0, step=0.01), FloatSlider(value=0.7, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 引入YOLO模型用于图像目标检测\n",
    "from ultralytics import YOLO\n",
    "# matplotlib.pyplot 用于显示和渲染图像\n",
    "import matplotlib.pyplot as plt\n",
    "# cv2 用于处理和转化图像格式\n",
    "import cv2\n",
    "# ipywidgets 提供了交互性组件，如滑动条和复选框等\n",
    "from ipywidgets import interact, FloatSlider, Checkbox\n",
    "\n",
    "# 加载YOLO模型，进行目标检测\n",
    "model = YOLO('../weights/yolov8x-seg.pt')\n",
    "\n",
    "# 定义一个函数，接受阈值参数和标签选项，进行目标检测并绘图显示结果\n",
    "def predict_and_plot(conf, iou, boxes, masks):\n",
    "    # 根据输入参数，对指定图片进行目标检测，返回检测结果\n",
    "    results = model.predict(source=\"../assets/people_walking.jpg\", conf=conf, iou=iou, verbose=False)\n",
    "    # 根据检测结果和输入参数，生成标注后的图片\n",
    "    res_plotted = results[0].plot(boxes=boxes, masks=masks)\n",
    "    # 将BGR格式的图像转为RGB格式，以便matplotlib正确显示\n",
    "    res_plotted_rgb = cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)\n",
    "    plt.imshow(res_plotted_rgb) # 使用matplotlib显示图片\n",
    "    plt.show()\n",
    "\n",
    "# 创建滑动条和复选框，用于动态调整参数和选项，并显示目标检测结果\n",
    "interact(predict_and_plot, \n",
    "         conf=FloatSlider(min=0, max=1, step=0.01, value=0.25),  # 置信度阈值滑动条\n",
    "         iou=FloatSlider(min=0, max=1, step=0.01, value=0.7),  # 交并比阈值滑动条\n",
    "         boxes=Checkbox(value=True, description='Boxes'),  # 是否显示目标框复选框\n",
    "         masks=Checkbox(value=False, description='Masks'));  # 是否显示目标掩码复选框\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolo_nas",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
